package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.utils.JwtUtils;
import com.atguigu.srb.common.utils.MD5;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.entity.UserLoginRecord;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.core.service.UserLoginRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author Mr.bilal
 * @since 2023-07-18
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    RedisTemplate redisTemplate;

    @Resource
    UserAccountMapper userAccountMapper;

    @Autowired
    UserLoginRecordService userLoginRecordService;

    @Override
    public void register(RegisterVO registerVO) {
        //验证码证明
        String cacheCode = (String) redisTemplate.opsForValue().get("srb:sms:code:"+registerVO.getMobile());
        Assert.isTrue(cacheCode.equals(registerVO.getCode()), ResponseEnum.CODE_ERROR);

        //保存用户信息的user_info表
        UserInfo userInfo = new UserInfo();
        userInfo.setUserType(registerVO.getUserType());
        userInfo.setMobile(registerVO.getMobile());
        userInfo.setPassword(MD5.encrypt(registerVO.getPassword()));
        userInfo.setName(registerVO.getMobile());
        userInfo.setNickName(registerVO.getMobile());
        baseMapper.insert(userInfo);

        //生成一条空的user_account信息
        Long userInfoId = userInfo.getId();
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userInfoId);
        userAccountMapper.insert(userAccount);
    }

    @Override
    public UserInfoVO login(LoginVO loginVO, String ip) {

        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Integer userType = loginVO.getUserType();

        //验证用户名和密码是否正确
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type",userType);
        queryWrapper.eq("mobile",mobile);
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        Assert.isTrue(userInfo.getPassword().equals(MD5.encrypt(password)),ResponseEnum.LOGIN_PASSWORD_ERROR);
        //生产token
        UserInfoVO userInfoVO = new UserInfoVO();
        String token = JwtUtils.createToken(userInfo.getId(), userInfo.getName());
        userInfoVO.setToken(token);
        userInfoVO.setUserType(userType);
        userInfoVO.setName(userInfo.getName());
        userInfoVO.setNickName(userInfo.getNickName());
        userInfoVO.setMobile(mobile);
        userInfoVO.setHeadImg(userInfo.getHeadImg());
        //记录登录日志
        UserLoginRecord loginRecord = new UserLoginRecord();
        loginRecord.setIp(ip);
        loginRecord.setUserId(userInfo.getId());
        userLoginRecordService.save(loginRecord);
        return userInfoVO;
    }

    @Override
    public boolean isMobileExist(String mobile) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(queryWrapper);
        return integer > 0;
    }

    @Override
    public UserInfo getUserInfoByMobile(String mobile) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("mobile",mobile);
        UserInfo userInfo = baseMapper.selectOne(userInfoQueryWrapper);
        if (userInfo == null) {
            return new UserInfo();
        }
        return userInfo;
    }
}
